from pydantic import BaseModel, Field, ConfigDict
from typing import Optional, Dict, Any, List
from datetime import datetime
from decimal import Decimal


class DetectionLogBase(BaseModel):
    """检测日志基础模型"""
    # 关联信息
    drone_id: int = Field(..., description="无人机ID")
    task_id: Optional[int] = Field(None, description="任务ID")
    flight_log_id: Optional[int] = Field(None, description="飞行日志ID")
    detection_target_id: Optional[int] = Field(None, description="检测目标ID")
    detection_model_id: Optional[int] = Field(None, description="检测模型ID")
    model_version_id: Optional[int] = Field(None, description="模型版本ID")
    
    # 检测位置信息
    latitude: float = Field(..., description="纬度")
    longitude: float = Field(..., description="经度")
    altitude: float = Field(..., description="高度（米）")
    
    # 检测结果信息
    confidence: float = Field(..., ge=0, le=100, description="置信度（0-100）")
    detection_result: Optional[Dict[str, Any]] = Field(None, description="详细检测结果")
    is_positive: bool = Field(True, description="是否为正样本")
    
    # 照片信息
    photo_path: Optional[str] = Field(None, description="照片存储路径")
    photo_thumbnail_path: Optional[str] = Field(None, description="缩略图路径")
    photo_metadata: Optional[Dict[str, Any]] = Field(None, description="照片元数据")
    
    # 附加信息
    notes: Optional[str] = Field(None, description="备注信息")
    processed: bool = Field(False, description="是否已处理")


class DetectionLogCreate(DetectionLogBase):
    """创建检测日志请求模型"""
    photo_time: Optional[datetime] = Field(None, description="触发拍照时间")


class DetectionLogUpdate(BaseModel):
    """更新检测日志请求模型"""
    task_id: Optional[int] = Field(None, description="任务ID")
    flight_log_id: Optional[int] = Field(None, description="飞行日志ID")
    detection_target_id: Optional[int] = Field(None, description="检测目标ID")
    detection_model_id: Optional[int] = Field(None, description="检测模型ID")
    model_version_id: Optional[int] = Field(None, description="模型版本ID")
    confidence: Optional[float] = Field(None, ge=0, le=100, description="置信度（0-100）")
    detection_result: Optional[Dict[str, Any]] = Field(None, description="详细检测结果")
    is_positive: Optional[bool] = Field(None, description="是否为正样本")
    photo_path: Optional[str] = Field(None, description="照片存储路径")
    photo_thumbnail_path: Optional[str] = Field(None, description="缩略图路径")
    photo_metadata: Optional[Dict[str, Any]] = Field(None, description="照片元数据")
    notes: Optional[str] = Field(None, description="备注信息")
    processed: Optional[bool] = Field(None, description="是否已处理")


class DetectionLog(DetectionLogBase):
    """检测日志响应模型"""
    model_config = ConfigDict(from_attributes=True)
    
    id: int = Field(..., description="检测日志ID")
    detection_time: datetime = Field(..., description="检测时间")
    photo_time: Optional[datetime] = Field(None, description="触发拍照时间")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    
    # 关联信息（用于响应）
    drone_name: Optional[str] = Field(None, description="无人机名称")
    task_name: Optional[str] = Field(None, description="任务名称")
    target_name: Optional[str] = Field(None, description="检测目标名称")
    model_name: Optional[str] = Field(None, description="检测模型名称")
    model_version_name: Optional[str] = Field(None, description="模型版本名称")


class DetectionLogList(BaseModel):
    """检测日志列表响应模型"""
    code: int = Field(200, description="响应代码")
    message: str = Field("获取成功", description="响应消息")
    data: Dict[str, Any] = Field(..., description="数据")


class DetectionLogDetail(BaseModel):
    """检测日志详情响应模型"""
    code: int = Field(200, description="响应代码")
    message: str = Field("获取成功", description="响应消息")
    data: DetectionLog = Field(..., description="检测日志详情")


class DetectionLogStatistics(BaseModel):
    """检测统计数据响应模型"""
    code: int = Field(200, description="响应代码")
    message: str = Field("获取成功", description="响应消息")
    data: Dict[str, Any] = Field(..., description="统计数据")